package opdracht3;

import java.util.Iterator;

public class RecList<T> implements Iterable<T> {

        private RecList<T>  next;
        private T           value;

        public T getObject() 
        {
                return value;
        }
        
        public boolean isEmpty() 
        {
                return (next == null);
        }
        
        public int size() 
        {
                return isEmpty() ? 0 : 1 + next.size();
        }
        
        public void remove(T object) 
        {
        	if(isEmpty()) {return;}
            if (value == object) 
            {
                   value = next.value;
                   next = next.next;
            } else 
            {
                   next.remove(object);
            }
        }
        
        public void add(T object) 
        {
        	if( object == value ) {return;}
                if (isEmpty())
                {
                        value = object;
                        next = new RecList<T>();
                } 
                else 
                {
                        next.add(object);
                }
        }
        public void addAfter(T object, T afterObject) 
        {
                if(isEmpty()) {return;}
                if (value == afterObject) 
                {
                		next = new RecList<T>();
                        RecList<T> newNext = new RecList<T>();
                        newNext.value = object;
                        newNext.next = next;
                        next = newNext;
                } else 
                {
                        next.addAfter(object, afterObject);
                }
        }
        public boolean contains(T object) 
        {
                if(isEmpty()) {return false;}
                return value.equals(object) ? true : next.contains(object);
        }
        public String toString() 
        {
        	if(isEmpty()) {return "";}
                return value.toString() + next;
        }
        public Iterator<T> iterator() 
        {
                return new RecListIterator<T>();
        }

        private class RecListIterator<T> implements Iterator<T> 
        {
                private RecList<T> current;

                public RecListIterator() 
                {
                        current = (RecList<T>) RecList.this;
                }
                public boolean hasNext() 
                {
                        return !current.isEmpty();
                }
                public T next() 
                {
                        T currentObject = current.getObject();
                        current = current.next;
                        return currentObject;
                }
                public void remove()
                {
                        
                }
        }

}
